//
//  FamilyManageVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/28.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ChangeFamilyVC.h"
#import "MyFamilyTableVCell.h"
#import "Sizes.h"
#import "FamilyDetailVC.h"
#import "FamilyOptionView.h"
#import "FamilyDescVC.h"
#import "ChangeFamilyVC.h"
#import "UserTool.h"
#import "UserModel.h"
#import "FamilyService.h"
#import "FamilyNameVC.h"
#import "FamilyDetailVC.h"
#import "BlacklistVC.h"
#import "FamilymemberVC.h"
#import "Colors.h"
#import "WHToast.h"
#import "Fonts.h"
#import "TYMButton.h"
#import "TYMBaseTableViewCell.h"
#import "FamilyManageTwoCell.h"
#import "FamilyManageOneCell.h"
#import "GroupShadowTableView.h"
#import "FamilyManageThreeCell.h"
@interface ChangeFamilyVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;


@property(nonatomic,strong) FamilyService *familyService;

@end

@implementation ChangeFamilyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"家庭切换"];
    [self setTable];

}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 15, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.groupShadowDelegate = self;
    _tableView.groupShadowDataSource = self;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.headTitles = @[@"我的家庭列表"];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyFamilyTableVCell  class]) bundle:nil] forCellReuseIdentifier:[MyFamilyTableVCell identifier]];
    
    [self.view addSubview:_tableView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)]; view.backgroundColor = [UIColor redColor]; self.tableView.tableHeaderView = view;
    
}

-(void)rightFunc:(id)sender{
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDataArrayWithWeb];
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)getDataArrayWithWeb{
    //家长
    TYMWeak(self);
    [self.familyService getFindFamilysCompletion:^(NSError * _Nonnull error, NSArray<FamilyModel *> * _Nonnull array) {
        if(weakself.dataArray.count){
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:array];
        [self.tableView reloadData];
    }];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MyFamilyTableVCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyFamilyTableVCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyFamilyTableVCell identifier]];
    FamilyModel *familyModel = self.dataArray[indexPath.row];
    cell.familyModel = familyModel;
    
    if (familyModel.familyId == [UserTool getCurrentFamily].familyId) {
        cell.currentLab.hidden = NO;
        cell.righImage.hidden = YES;
    }else{
        cell.currentLab.hidden = YES;
        cell.righImage.hidden = NO;
    }
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArray.count) {
        FamilyModel *model = self.dataArray[indexPath.row];
        if (model.familyId == [UserTool getCurrentFamily].familyId) {
            return;
        }
        FamilyDetailVC *detailVC = [[FamilyDetailVC alloc] init];
        detailVC.familyModel = model;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
